package com.ys.controller.api.storage;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.ys.config.MenuId;
import com.ys.entity.amazonorder.TpAmazonShopOrderDaily;
import com.ys.entity.storage.TccAllocateCancel;
import com.ys.entity.storage.TccAllocateCancelDetail;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.flow.YspTaskService;
import com.ys.service.storage.TccAllocateCancelService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.ExportExcelUtils;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/tccAllocateCancel")
public class ApiInterTccAllocateCancelController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private TccAllocateCancelService tccAllocateCancelService;
	@Autowired
	private YspTaskService yspTaskService;
	
	@Autowired
	private CommonApiService<TccAllocateCancelDetail> commonApiServiceDetail;

    /**
     * 设置查询的cond的sql设置
     */
    private TccAllocateCancelDetail setSeachSql(TccAllocateCancelDetail cond){
        cond.setStatus(1);//数据过滤
		
		cond = commonApiServiceDetail.dataAuth(MenuId.TCC_AllocateCancel, cond);
		
		cond.addLeftJoin("TCC_AllocateCancel B ON B.Bid = A.BidAllocateCancel")
			.addField("B.Bid bid")
			.addField("B.EffectStock effectStock")
			.addField("B.Remark remark");
			
		cond.addLeftJoin("TS_UserChange C on C.Id = B.CreateChangeId")
			.addField("C.UserId createUserId");
		
		cond.addLeftJoin("TCC_AllocateDetail D on D.Id = A.AllocateDetailId")
			.addField("D.BidAllocate bidAllocate")
			.addField("D.VgsbmFrom vgsbmFrom")
			.addField("D.VgsjcFrom vgsjcFrom")
			.addField("D.VgsbmTo vgsbmTo")
			.addField("D.VgsjcTo vgsjcTo")
			.addField("D.ProductCode productCode")
			.addField("D.ProductName productName")
			.addField("D.Num allocateNum")
			.addField("D.DistributeNum distributeNum") // 已分配数量
			.addField("D.AllocateCancelNum allocateCancelNum"); // 已取消数量
		
		cond.addLeftJoin("TCC_Allocate E on E.Bid = D.BidAllocate")
			.addField("E.Type allocateType");
		
		cond.addLeftJoin("YS_ProcInst F on F.Bid = A.BidAllocateCancel")
			.addField("F.UpdateTime auditTime");

		cond.addLeftJoin("THR_OperateCompanyApply G ON  G.VGsBm = D.VgsbmTo")
				.addField("G.CUser cUser");
		
		cond.setOrderby("A.Id desc");
        return  cond;
    }

	@RequiresPermissions("tccAllocateCancel:view")
	@RequestMapping("/searchDetail")
    @ResponseBody
	public LayuiTableVoJA searchDetail(@ModelAttribute TccAllocateCancelDetail cond){
		
        cond = setSeachSql(cond);
        
		LayuiTableVoJA vo = commonApiServiceDetail.searchLay(cond);
		return vo;
	}
	
	/**
	 * 新增
	 * @throws Exception 
	 */
	@RequiresPermissions("tccAllocateCancel:add")
	@PostMapping("/add")
    @ResponseBody
	public R add(HttpServletRequest request,
			String bid, String remark, String subData) throws Exception{
		
		return tccAllocateCancelService.add(bid, remark, subData, MenuId.TCC_AllocateCancel);
	}
	
	/**
	 * 新增
	 * @throws Exception 
	 */
	@PostMapping("/edit")
    @ResponseBody
	public R edit(HttpServletRequest request,
			String bid, String remark, String subData) throws Exception{
		
		return tccAllocateCancelService.edit(bid, remark, subData, MenuId.TCC_AllocateCancel);
	}
	
	/**
	 * 流转通过
	 */
	@RequiresPermissions("tccAllocateCancel:add")
	@PostMapping("/flowPass")
    @ResponseBody
	public R flowPass(HttpServletRequest request, Long id){
		try {
			return tccAllocateCancelService.flowPass(id, ShiroUtils.getUserId());
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			return R.error(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}
	
	/**
	 * 流转驳回
	 */
	@RequiresPermissions("tccAllocateCancel:add")
	@PostMapping("/flowRefuse")
    @ResponseBody
	public R flowRefuse(HttpServletRequest request, Long id, String reason){
		
		TccAllocateCancel entity = new TccAllocateCancel().setId(id).queryFirst();
		String bid = entity.getBid();
		try {
			return yspTaskService.flowRefuse(bid, reason, ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}


	/**
	 * 流转驳回
	 */
	@RequiresPermissions("tccAllocateCancel:add")
	@PostMapping("/flowInit")
	@ResponseBody
	public R flowInit(HttpServletRequest request, Long id, String reason){

		TccAllocateCancel entity = new TccAllocateCancel().setId(id).queryFirst();
		String bid = entity.getBid();
		try {
			return yspTaskService.flowInit(bid, reason, ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}
	
	/**
	 * 删除
	 * @throws Exception 
	 */
	@RequiresPermissions("tccAllocateCancel:delete")
	@PostMapping("/delete")
    @ResponseBody
	public R delete(Long id){
		
		try {
			return tccAllocateCancelService.deleteTrue(id);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("操作失败，请联系客服");
		}
	}
	
	/*
	 * 导出
	 */
	@RequiresPermissions("tccAllocateCancel:export")
	@RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TccAllocateCancelDetail cond, String conditions) throws Exception{
		
        cond = setSeachSql(cond);
		
		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.queryMap();
        
    	List<YsCol> listField = new YsCol()
    			.setMenuId(MenuId.TCC_AllocateCancel)
    			.setFrontShow(YsColEnum.FrontShow.SHOW.value)
    			.setOrderby("A.SortNo asc")
    			.queryList();
    	
    	
        ExportExcelUtils<TpAmazonShopOrderDaily> util = new ExportExcelUtils<TpAmazonShopOrderDaily>(new TpAmazonShopOrderDaily());
        
        String toFilePath = util.exportExcel(listData, listField, "预约调拨取消单");
        UploadDto dto = commonApiServiceDetail.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }
}
